<?php

namespace app\common\validate;

use think\Validate;

class Infrastructure extends Validate
{

    /**
     * 验证规则
     */
    protected $rule = [
        'id' => 'require',
        'title' => 'require|uniqueTitle',
        'icon' => 'require'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        "id.require" => "缺少参数id",
        "title.require" => "请输入标题",
        "title.uniqueTitle" => "配套设施已存在",
        "icon.require" => "请上传图片",
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add' => ['title', 'icon'],
        'update' => ['title', 'icon', 'id'],
    ];

    public function uniqueTitle($value, $rule, $row)
    {
        $model = new \app\common\model\Infrastructure();
        $query = $model->where("title", $value);
        if (!empty($row['id'])) $query->where("id", "<>", $row['id']);
        $findUser = $query->find();
        if (empty($findUser)) return true;
        return false;
    }

}
